package openjdk.stream;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

public class GrpByAndThenFindFirst {

    private static int mod = 24/6;
    private static List<LinkedHashMap<String,Object>> list = Arrays.asList(
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-01");put("val",Double.valueOf("1.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-01");put("val",Double.valueOf("1.13"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-01");put("val",Double.valueOf("1.11"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-02");put("val",Double.valueOf("2.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-02");put("val",Double.valueOf("2.22"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-03");put("val",Double.valueOf("3.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-03");put("val",Double.valueOf("3.33"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-04");put("val",Double.valueOf("4.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-04");put("val",Double.valueOf("4.44"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-05");put("val",Double.valueOf("5.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-05");put("val",Double.valueOf("5.55"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-06");put("val",Double.valueOf("6.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-06");put("val",Double.valueOf("3.27"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-06");put("val",Double.valueOf("6.66"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-07");put("val",Double.valueOf("7.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-07");put("val",Double.valueOf("3.50"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-07");put("val",Double.valueOf("7.77"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-08");put("val",Double.valueOf("8.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-08-08");put("val",Double.valueOf("8.88"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-09-09");put("val",Double.valueOf("9.00"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2018-09-09");put("val",Double.valueOf("9.99"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2019-08-07");put("val",Double.valueOf("7.77"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2019-08-07");put("val",Double.valueOf("7.77"));}},
            new LinkedHashMap<String,Object>(){{put("ctime","2019-08-07");put("val",Double.valueOf("7.77"));}}
    );

    private static LocalDate td3M = LocalDate.now().minusMonths(3);

    public static void main(String[] args) {

        getPeriod();

        List<LinkedHashMap<String,Object>> lst = list.stream().filter(o->{
            String str = (String)o.get("ctime");
            return td3M.toString().compareTo(str)>0;
        }).collect(Collectors.toList());

        // 每6小时取一根
        List<LinkedHashMap<String,Object>> lsx = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i% mod == 0){
                lsx.add(list.get(i));
            }
        }


        List ll = getPeriod2();
        System.out.println(ll.size());
    }




    private static  List<Map<String,Object>> getPeriod(){
        List<LinkedHashMap<String,Object>> lst = list.stream().filter(o->{
            String str = (String)o.get("ctime");
            return td3M.toString().compareTo(str)>0;
        }).collect(Collectors.toList());


        Object map = lst.stream().collect(Collectors.groupingBy(o -> o.get("ctime")));
        Map<String,List<Map<String,Object>>> mp = (Map<String,List<Map<String,Object>>>) map;

        Map<String,List<Map<String,Object>>> mpp =mp.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldVal, newVal)-> oldVal,LinkedHashMap::new));


        List lsx =  mpp.values().stream().map(o->o.get(0)).collect(Collectors.toList()) ;
        lsx.forEach(o-> System.out.println(o));


        List llss  = getPeriod2();

        System.out.println(llss.size());
        return lsx;
    }



    private static  List<Map<String,Object>> getPeriod1(){
        return list.stream()
                .filter(o-> td3M.toString().compareTo(o.get("ctime").toString())>0)
                .collect(Collectors.toList())
                .stream().collect(Collectors.groupingBy(e -> e.get("ctime")))
                .values().stream().map(o->o.get(0)).collect(Collectors.toList());
    }

    // 取每天的最后一个数据
    private static  List<Map<String,Object>> getPeriod2(){
        return list.stream()
                .filter(o-> td3M.toString().compareTo(o.get("ctime").toString())>0)
                .collect(Collectors.groupingBy(e -> e.get("ctime")))
                .entrySet().stream()
                .sorted(Comparator.comparing(m -> m.getKey().toString()))
                .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldVal,newVal)->oldVal,LinkedHashMap::new))
                .values().stream().map(o->o.get(0)).collect(Collectors.toList());
    }
}

